<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="电影名" prop="movieName">
        <el-input
          v-model="queryParams.movieName"
          placeholder="请输入电影名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电影类型" prop="movieType">
        <el-select v-model="queryParams.movieType" placeholder="请选择电影类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="电影国家" prop="movieCountry">
        <el-input
          v-model="queryParams.movieCountry"
          placeholder="请输入电影国家"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上映时间" prop="movieTime">
        <el-input
          v-model="queryParams.movieTime"
          placeholder="请输入上映时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时长" prop="movieDuration">
        <el-input
          v-model="queryParams.movieDuration"
          placeholder="请输入时长"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="封面" prop="movieImg">
        <el-input
          v-model="queryParams.movieImg"
          placeholder="请输入封面"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="豆瓣评分" prop="movieRank">
        <el-input
          v-model="queryParams.movieRank"
          placeholder="请输入豆瓣评分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="up时间" prop="upTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.upTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择up时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="up数量" prop="upNum">
        <el-input
          v-model="queryParams.upNum"
          placeholder="请输入up数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="up状态" prop="upState">
        <el-input
          v-model="queryParams.upState"
          placeholder="请输入up状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gt:gtmovie:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gt:gtmovie:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gt:gtmovie:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gt:gtmovie:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="gtmovieList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="up状态" align="center" prop="movieId" />
      <el-table-column label="电影名" align="center" prop="movieName" />
      <el-table-column label="电影类型" align="center" prop="movieType" />
      <el-table-column label="电影国家" align="center" prop="movieCountry" />
      <el-table-column label="上映时间" align="center" prop="movieTime" />
      <el-table-column label="时长" align="center" prop="movieDuration" />
      <el-table-column label="封面" align="center" prop="movieImg" />
      <el-table-column label="多图" align="center" prop="movieImgs" />
      <el-table-column label="豆瓣评分" align="center" prop="movieRank" />
      <el-table-column label="简介" align="center" prop="movieIntro" />
      <el-table-column label="up时间" align="center" prop="upTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="up数量" align="center" prop="upNum" />
      <el-table-column label="up状态" align="center" prop="upState" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gt:gtmovie:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gt:gtmovie:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电影卡片对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="电影名" prop="movieName">
          <el-input v-model="form.movieName" placeholder="请输入电影名" />
        </el-form-item>
        <el-form-item label="电影类型">
          <el-select v-model="form.movieType" placeholder="请选择电影类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="电影国家" prop="movieCountry">
          <el-input v-model="form.movieCountry" placeholder="请输入电影国家" />
        </el-form-item>
        <el-form-item label="上映时间" prop="movieTime">
          <el-input v-model="form.movieTime" placeholder="请输入上映时间" />
        </el-form-item>
        <el-form-item label="时长" prop="movieDuration">
          <el-input v-model="form.movieDuration" placeholder="请输入时长" />
        </el-form-item>
        <el-form-item label="封面" prop="movieImg">
          <el-input v-model="form.movieImg" placeholder="请输入封面" />
        </el-form-item>
        <el-form-item label="多图" prop="movieImgs">
          <el-input v-model="form.movieImgs" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="豆瓣评分" prop="movieRank">
          <el-input v-model="form.movieRank" placeholder="请输入豆瓣评分" />
        </el-form-item>
        <el-form-item label="简介" prop="movieIntro">
          <el-input v-model="form.movieIntro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="up时间" prop="upTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.upTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择up时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="up数量" prop="upNum">
          <el-input v-model="form.upNum" placeholder="请输入up数量" />
        </el-form-item>
        <el-form-item label="up状态" prop="upState">
          <el-input v-model="form.upState" placeholder="请输入up状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listgtmovie, getgtmovie, delgtmovie, addgtmovie, updategtmovie } from "@/api/gt/gtmovie";

export default {
  name: "gtmovie",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 电影卡片表格数据
      gtmovieList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        movieName: undefined,
        movieType: undefined,
        movieCountry: undefined,
        movieTime: undefined,
        movieDuration: undefined,
        movieImg: undefined,
        movieImgs: undefined,
        movieRank: undefined,
        movieIntro: undefined,
        upTime: undefined,
        upNum: undefined,
        upState: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询电影卡片列表 */
    getList() {
      this.loading = true;
      listgtmovie(this.queryParams).then(response => {
        this.gtmovieList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        movieId: undefined,
        movieName: undefined,
        movieType: undefined,
        movieCountry: undefined,
        movieTime: undefined,
        movieDuration: undefined,
        movieImg: undefined,
        movieImgs: undefined,
        movieRank: undefined,
        movieIntro: undefined,
        upTime: undefined,
        upNum: undefined,
        createTime: undefined,
        upState: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.movieId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电影卡片";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const movieId = row.movieId || this.ids
      getgtmovie(movieId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电影卡片";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.movieId != undefined) {
            updategtmovie(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addgtmovie(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const movieIds = row.movieId || this.ids;
      this.$confirm('是否确认删除电影卡片编号为"' + movieIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delgtmovie(movieIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gt/gtmovie/export', {
        ...this.queryParams
      }, `gt_gtmovie.xlsx`)
    }
  }
};
</script>
